/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBLager;
import java.sql.*;
import ModelLager.*;
import java.util.ArrayList;
/**
 *
 * @author Jonas Kristensen
 */
public class DbPartOrderP {
    private Connection con;
    private PreparedStatement pStmtSelect;
    
    public DbPartOrderP()
    {
        con = DbConnection2.getInstance().getDBcon();
    }
    
    public PreparedStatement getPreparedStmt()
    {
        try
        {
              String query = "SELECT * "
                           + "FROM PartOrderP "
                           + "WHERE orderID = ?";
                      
            System.out.println("Preparestatement " + query);
            pStmtSelect = con.prepareStatement(query);
            pStmtSelect.setQueryTimeout(5);
        }
        catch(Exception e)
        {
            e.getMessage();
        }
        return pStmtSelect;
    }
    
    public PartOrderP buildPartOrderP(ResultSet results)
    {
        PartOrderP pObj = new PartOrderP();
        SalesOrderP salesObj = new SalesOrderP();
        ProductP productObj = new ProductP();
        
        pObj.setSalesOrder(salesObj);
        pObj.setProduct(productObj);
        try
        {
            pObj.setAmountItem(results.getInt("amountItem"));
            productObj.setProductID(results.getInt("productID"));
            salesObj.setOrderID(results.getInt("orderID"));
        }
        catch(Exception e)
        {
            System.out.println("Error in building partorder object");
        }
        return pObj;
    }
    
    public ArrayList<PartOrderP> findByOrderIDPreQuery(int orderID, boolean retriveAssociation)
    {
        ArrayList<PartOrderP> partOrders = new ArrayList<PartOrderP>();
        
        System.out.println("Find with preCompiled");
        pStmtSelect = getPreparedStmt();
        
        PartOrderP pObj = null;
        ResultSet results;
        try
        {
            pStmtSelect.clearParameters();
            pStmtSelect.setInt(1, orderID);
            results = pStmtSelect.executeQuery();
            while(results.next())
            {
                pObj = buildPartOrderP(results);
                partOrders.add(pObj);
            }
            pStmtSelect.close();
            if(retriveAssociation)
            {
                DbProductP dbProduct = new DbProductP();
                for(PartOrderP p : partOrders)
                {
                    p.setProduct(dbProduct.findProduct(p.getProduct().getProductID()));
                }
            }
                
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        
        return partOrders;

    }
        public ArrayList<PartOrderP> getAllPartOrders()
    {      
            PreparedStatement preStmnt = null;          
            ArrayList<PartOrderP> list = new ArrayList<PartOrderP>();
            ResultSet results;
            PartOrderP cObj = null;
            try{    
            String query = "SELECT * FROM PartOrderP";     
            preStmnt = con.prepareStatement(query);
            results = preStmnt.executeQuery();
            
            while(results.next()){
                 cObj = buildPartOrderP(results);
                 list.add(cObj); 
            }
            preStmnt.close();
            }catch(SQLException SQLe)
            {
                System.out.println("SQL Exception: " + SQLe);
            }
            catch(Exception e){
                System.out.println("Fejl i findAllPartOrder(): " + e.getMessage());
                
            }
            
            return list;
        
    }
    
    public int insertPartOrderP(PartOrderP pObj) throws SQLException, Exception
    {
        int rc = -1;
        PreparedStatement pstmt = null;
        String insert = "INSERT INTO PartOrderP (productID, orderID, amountItem) VALUES (?,?,?)";
        System.out.println(insert);
        try
        {
            pstmt = con.prepareStatement(insert);
            pstmt.setQueryTimeout(5);
            pstmt.setInt(1, pObj.getProduct().getProductID());
            pstmt.setInt(2, pObj.getSalesOrder().getOrderID());
            pstmt.setInt(3, pObj.getAmountItem());
            
            rc = pstmt.executeUpdate();
            pstmt.close();
        }
        catch(SQLException sqlE)
        {
            System.out.println("SQL ERROR: " + sqlE.getMessage());
            throw new SQLException();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            throw new Exception();        
        }
        return rc;
    }
    
    public int deletePartOrderP(int productID, int orderID) throws SQLException, Exception
    {
        int rc = -1;
        PreparedStatement pstmt = null;
        String delete = "DELETE FROM PartOrderP WHERE productID = ? AND orderID = ?";
        System.out.println(delete);
        try
        {
            pstmt = con.prepareStatement(delete);
            pstmt.setQueryTimeout(5);
            pstmt.setInt(1, productID);
            pstmt.setInt(2, orderID);
            
            rc = pstmt.executeUpdate();
            
            pstmt.close();
        }
        catch(SQLException sqlE)
        {
            System.out.println("SQL ERROR: " + sqlE.getMessage());
            throw new SQLException();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            throw new Exception();        
        }
        return rc;
    }

}
